﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class SupplierUserController : BaseController<supplieruser>
    {
        //
        // GET: /SupplierUser/


        public override BLL.BaseBll<supplieruser> bll
        {
            get
            {
                return new SupplierUserBll();
            }

        }
        public class PancakeUser
        {
            public int id;
            public string name;
            public string username;
            public string type;
            public string status;
            public string password;
        }

        List<PancakeUser> modelList = new List<PancakeUser>();


        public  ActionResult Get2(int page, int rows, supplieruser model)
        {
            var list = bll.Search().Select(x => new {x.id ,x.name,x.password,x.username,x.status,x.type}).ToList();
            foreach (var item in list)
            {    // id  name password username status   type
                PancakeUser usermodel = new PancakeUser();
                usermodel.id = item.id;
                usermodel.name = item.name;
                usermodel.username = item.username;
                usermodel.status = Convert.ToInt32(item.status) == 1 ? "有效" : "无效";
                modelList.Add(usermodel);
            }
            return Json(new {rows=list,total=list.Count});  
        }

        public  ActionResult Save2(supplieruser model)
        {
            string msg = "";
            int result = 0;
            int jieguo = 1;
            if (model.username != "" && model.username !=null && model.password != ""&& model.password != null)
            {
                var count = bll.Search(x => x.username == model.username).Count();
                //判断该用户是否存在  
                if (count == 0)
                {
                    model.password = MD5Encrypt32(model.password);
                    model.status ="1";
                    if (bll.Add(model) > 0) 
                    {
                        msg = "添加成功";
                        result = 1;
                        jieguo = 1;
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                    else
                    {
                        jieguo = 1;
                        msg = "添加失败";
                        return Json(new { jieguo = jieguo, state = result, msg = msg });
                    }
                }
                else
                {
                    jieguo = 0;
                    msg = "该用户名已经存在，请重新填写";
                    return Json(new { jieguo = jieguo, state = result, msg = msg });
                }
            }
            else
            {
                jieguo = 0;
                msg = "请填写完整";
                return Json(new { jieguo = jieguo, state = result, msg = msg });
            }
        }

        //MD5加密方法  32位加密
        public static string MD5Encrypt32(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create(); //实例化一个md5对像
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
    }
}
